/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.javacommunity.model.dao.impl;

import com.javacommunity.hibernate.HibernateUtil;
import com.javacommunity.model.dao.TagDAO;
import com.javacommunity.model.entity.Tag;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author Cong Pham Dinh
 */
public class TagDAOImpl implements TagDAO{

    private static TagDAOImpl tagDAO;

	public static TagDAOImpl getInstance() {
		if (tagDAO == null) {
			tagDAO = new TagDAOImpl();
		}
		return tagDAO;
	}

	private Session session;
	private Transaction ts;

	@Override
	public Tag getTagById(int tagId) {
		session = HibernateUtil.getSessionFactory().openSession();
		Tag tag = null;
		try {
			ts = session.beginTransaction();
			tag = (Tag) session.get(Tag.class, tagId);
			ts.commit();
		} catch (HibernateException e) {
			if (ts != null) {
				ts.rollback();
			}
			e.printStackTrace();
		} finally {
			session.close();
		}
		return tag;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Tag> getAllTags(int param) {
		session = HibernateUtil.getSessionFactory().openSession();
		List<Tag> tags = null;
		try {
			ts = session.beginTransaction();
                        Query query = session.createSQLQuery("CALL sp_GetTags(?)").addEntity(Tag.class).setParameter(0, param);
			tags = query.list();
			ts.commit();
		} catch (HibernateException e) {
			if (ts != null) {
				ts.rollback();
			}
			e.printStackTrace();
		} finally {
			session.close();
		}
		return tags;
	}

	@Override
	public boolean createTag(Tag tag) {
		session = HibernateUtil.getSessionFactory().openSession();
		try {
			ts = session.beginTransaction();
			session.save(tag);
			ts.commit();
			return true;
		} catch (Exception e) {
			if (ts != null) {
				ts.rollback();
			}
			e.printStackTrace();
			return false;
		} finally {
			session.close();
		}
	}

	@Override
	public boolean updateTag(Tag tag) {
		session = HibernateUtil.getSessionFactory().openSession();
		try {
			ts = session.beginTransaction();
			session.update(tag);
			ts.commit();
			return true;
		} catch (Exception e) {
			if (ts != null) {
				ts.rollback();
			}
			e.printStackTrace();
			return false;
		} finally {
			session.close();
		}
	}

	@Override
	public boolean deleteTag(Tag tag) {
		session = HibernateUtil.getSessionFactory().openSession();
		try {
			ts = session.beginTransaction();
			session.delete(tag);
			ts.commit();
			return true;
		} catch (Exception e) {
			if (ts != null) {
				ts.rollback();
			}
			e.printStackTrace();
			return false;
		} finally {
			session.close();
		}
	}

	@Override
	public boolean removeTag(Tag tag) {
		session = HibernateUtil.getSessionFactory().openSession();
		try {
			ts = session.beginTransaction();
			String sql = "UPDATE Tag set active = false WHERE tagId = :tagId";
			Query query = session.createQuery(sql);
			query.setParameter("tagId", tag.getTagId());
			query.executeUpdate();
			ts.commit();
			return true;
		} catch (HibernateException e) {
			if (ts != null) {
				ts.rollback();
			}
			e.printStackTrace();
			return false;
		} finally {
			session.close();
		}
	}

	@Override
	public boolean restoreTag(Tag cat) {
		session = HibernateUtil.getSessionFactory().openSession();
		try {
			ts = session.beginTransaction();
			String sql = "UPDATE Tag set active = true WHERE tagId = :tagId";
			Query query = session.createQuery(sql);
			query.setParameter("tagId", cat.getTagId());
			query.executeUpdate();
			ts.commit();
			return true;
		} catch (HibernateException e) {
			if (ts != null) {
				ts.rollback();
			}
			e.printStackTrace();
			return false;
		} finally {
			session.close();
		}
	}
    
}
